package com.vilen.cms.controller;

import com.vilen.cms.auth.AuthClass;
import com.vilen.cms.model.ChannelTree;
import com.vilen.cms.model.Group;
import com.vilen.cms.service.IGroupService;
import com.vilen.cms.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by vilen on 2017/2/10.
 */
@Controller
@RequestMapping("admin/group")
@AuthClass
public class GroupController {
    //springmvc 可以设置成单例.而struts2 不行,
    //springmvc 的model 在方法里
    private IGroupService groupService;
    private IUserService userService;

    public IGroupService getGroupService() {
        return groupService;
    }

    @Inject
    public void setGroupService(IGroupService groupService) {
        this.groupService = groupService;
    }

    public IUserService getUserService() {
        return userService;
    }

    @Inject
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/groups")
    public String list(Model model) {
        model.addAttribute("datas", groupService.findGroup());
        return "group/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute(new Group());
        return "group/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@Validated Group group, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "group/add";
        }
        groupService.add(group);
        return "redirect:/admin/group/groups";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable int id, Model model) {
        model.addAttribute(groupService.load(id));
        return "group/update";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable int id, @Validated Group group, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "group/update";
        }
        Group ug = groupService.load(id);
        ug.setDescr(group.getDescr());
        ug.setName(group.getName());
        groupService.update(ug);
        return "redirect:/admin/group/groups";
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable int id) {
        groupService.delete(id);
        return "redirect:/admin/group/groups";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable int id, Model model) {
        model.addAttribute(groupService.load(id));
        model.addAttribute("us", userService.listGroupUsers(id));
        return "group/show";
    }

    @RequestMapping(value = "/clearUsers/{id}")
    public String clearGroupUsers(@PathVariable int id) {
        groupService.deleteGroupUsers(id);
        return "redirect:/admin/group/groups";
    }

    @RequestMapping(value = "/listChannels/{gid}")
    public String listChannels(@PathVariable int gid, Model model) {
        model.addAttribute(groupService.load(gid));
        return "group/listChannel";
    }

    @RequestMapping(value = "/groupTree/{gid}",method = RequestMethod.POST)
    public
    @ResponseBody
    List<ChannelTree> groupTree(@PathVariable Integer gid) {
        return groupService.generateGroupChannelTree(gid);
    }

    @RequestMapping(value = "/setChannels/{gid}")
    public String setChannels(@PathVariable int gid, Model model) {
        model.addAttribute(groupService.load(gid));
        model.addAttribute("cids", groupService.listGroupChannelIds(gid));
        return "group/setChannel";
    }
}
